﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;

namespace SerializationDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Person newPerson = new Person();
            Console.WriteLine("Get Cancer, could not live more than 80");
            newPerson.Disease = new DethDisease("Cancer", 80);

            //the normal way, the person will die at 80
            //NormalWay(newPerson);
            //the binary serializeway
            //BinarySerializeWay(newPerson);
            //the soap serializeway
            SoapSerializeWay(newPerson);

            Console.ReadLine();
        }

        static void NormalWay(Person newPerson)
        {
            while (true)
            {
                Console.WriteLine("A year pass");

                if (newPerson != null)
                {
                    if (newPerson.Live())
                        Console.WriteLine("My age is:" + newPerson.Age);
                    else
                    {
                        Console.WriteLine("Die");
                        break;
                    }
                }
            }
        }

        static void BinarySerializeWay(Person newPerson)
        {
            int yearAfterMedicine = 0;

            while (true)
            {
                Console.WriteLine("A year pass");
                if (newPerson == null)
                {
                    yearAfterMedicine++;
                    if (yearAfterMedicine > 10)
                    {
                        Console.WriteLine("get medicine, deserialize the person");
                        IFormatter formatter = new BinaryFormatter();
                        Stream stream = new FileStream("Person", FileMode.Open, FileAccess.Read, FileShare.Read);
                        newPerson = (Person)formatter.Deserialize(stream);
                        stream.Close();

                        newPerson.Medicine = "Cancer";
                    }
                    System.Threading.Thread.Sleep(1000);
                }

                //when no medicine and will die, will serialize the person
                if (newPerson != null && newPerson.Disease != null && newPerson.Age + 1 >= newPerson.Disease.Maxage && string.IsNullOrEmpty(newPerson.Medicine))
                {
                    Console.WriteLine("will die, serialize the person");
                    IFormatter formatter = new BinaryFormatter();
                    Stream stream = new FileStream("Person", FileMode.Create, FileAccess.Write, FileShare.None);
                    formatter.Serialize(stream, newPerson);
                    stream.Close();

                    newPerson = null;
                    System.Threading.Thread.Sleep(1000);
                }

                if (newPerson != null)
                {
                    if (newPerson.Live())
                        Console.WriteLine("My age is:" + newPerson.Age);
                    else
                    {
                        Console.WriteLine("Die");
                        break;
                    }
                }
            }
        }


        static void SoapSerializeWay(Person newPerson)
        {
            int yearAfterMedicine = 0;

            while (true)
            {
                Console.WriteLine("A year pass");
                if (newPerson == null)
                {
                    yearAfterMedicine++;
                    if (yearAfterMedicine > 10)
                    {
                        Console.WriteLine("get medicine, deserialize the person");
                        IFormatter formatter = new SoapFormatter();
                        Stream stream = new FileStream("Person.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
                        newPerson = (Person)formatter.Deserialize(stream);
                        stream.Close();

                        newPerson.Medicine = "Cancer";
                    }
                    System.Threading.Thread.Sleep(1000);
                }

                //when no medicine and will die, will serialize the person
                if (newPerson != null && newPerson.Disease != null && newPerson.Age + 1 >= newPerson.Disease.Maxage && string.IsNullOrEmpty(newPerson.Medicine))
                {
                    Console.WriteLine("will die, serialize the person");
                    IFormatter formatter = new SoapFormatter();
                    Stream stream = new FileStream("Person.xml", FileMode.Create, FileAccess.Write, FileShare.None);
                    formatter.Serialize(stream, newPerson);
                    stream.Close();

                    newPerson = null;
                    System.Threading.Thread.Sleep(1000);
                }

                if (newPerson != null)
                {
                    if (newPerson.Live())
                        Console.WriteLine("My age is:" + newPerson.Age);
                    else
                    {
                        Console.WriteLine("Die");
                        break;
                    }
                }
            }
        }
    }
}
